/*
 * =====================================================================================
 *
 *       Filename:  uart_test.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  07/03/15 10:01:40
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Andy (gk), andy_y_li@163.com
 *        Company:  RD
 *
 * =====================================================================================
 */

#include <stdio.h>
#include "uart.h"
#include <unistd.h>

typedef struct {
    int cmdNumber;
    const char *cmd;
} CommandList;

CommandList cmdList[5] = {
    {1,"state"},
    {2,"test pass"},
    {3,"test fail"},
    {4,"ask put pcba"},
    {5,"ask take pcba"},
};

void print_cmdlist(CommandList *pcmdList) 
{
    for (int i = 0; i < 5; i++) {
        printf("%5d%s\n", pcmdList[i].cmdNumber, pcmdList[i].cmd);
    }
}

const char *findCmd(int n)
{
   return cmdList[n - 1].cmd; 
}

int main(int argc, char *argv[])
{
    if (argc < 2) {
        printf("Usage: ./uart_test commandNuber\n");
        print_cmdlist(cmdList);
        return -1;
    }

    int n = atoi(argv[1]);
    const char *cmd = findCmd(n);
    Uart *port = new Uart("/dev/cu.wchusbserial1410", 9600);
    
    port->send_command(cmd);
    sleep(1);
    char *s = port->uart_read();
    printf("%s\n",(s?s:"Nothing to read!!!"));

    delete port;

    return 0;
}

